import { openapi } from '@elysiajs/openapi';
import z from 'zod';

import { config } from '@/config';
import { getServerUrls } from '@/config/utils';

/**
 * 创建 Openapi 配置
 */
export function createOpenapiConfig() {
  const { listen, advertise } = getServerUrls();

  return openapi({
    enabled: config.OPENAPI_ENABLED,
    path: config.OPENAPI_PATH,
    documentation: {
      info: {
        title: config.APP_NAME,
        version: config.APP_VERSION,
        description: config.APP_DESCRIPTION,
      },
      servers:
        listen !== advertise
          ? [
              {
                url: advertise,
                description: '服务器访问 URL',
              },
              {
                url: listen,
                description: '服务器监听 URL',
              },
            ]
          : [
              {
                url: advertise,
                description: '服务器 URL',
              },
            ],
    },
    mapJsonSchema: {
      zod: z.toJSONSchema.bind(z),
    },
  });
}
